<?php
/**
 * A Service that passes on the calls to its helper services relevant to Point of Sale.
 * Allows the helper services to be switched without effecting the client side code.
 * @copyright 2008 Nicolas Schudel
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */
class POSServices {
	
	private $testMode; // this allows different configuration settings for Unit Testing
	
	private $products;
	private $orderfactory;
	private $user;
	
	function __construct() {
		$parameters = func_get_args();
		if (isset($parameters[0]) && $parameters[0] == 1) {
			$this->testMode = true;	
		} else {
			$this->testMode = false;
			define('IS_ADMIN_FLAG', false);
		}
		
		$this->setPaths();
		$this->loadServices();
	}
	
	private function setPaths() {
		define('SERVICES_PATH', realpath(dirname(dirname(__FILE__))) . '/services/');
		define('DTO_PATH', SERVICES_PATH . 'dto/');
		define('INTERFACES_PATH', realpath(dirname(dirname(__FILE__))) . '/lib/interfaces/');
		define('INTEGRATION_PATH', realpath(dirname(dirname(__FILE__))) . '/lib/zencart/');
	}
	
	private function loadServices() {
		require_once(INTEGRATION_PATH . 'User.php'); // the user has to be loaded first for authentication
		$this->testMode ? require_once(TEST_CONFIGURATION_PATH . 'Configuration.php') : require_once(INTEGRATION_PATH . 'Configuration.php');
		require_once(INTEGRATION_PATH . 'ServiceLocator.php');
		require_once(INTEGRATION_PATH . 'ConnectionFactory.php');
		require_once(INTEGRATION_PATH . 'User.php');
		require_once(INTEGRATION_PATH . 'Currencies.php');
		require_once(INTEGRATION_PATH . 'Prices.php');
		require_once(INTEGRATION_PATH . 'Taxes.php');
		require_once(INTEGRATION_PATH . 'OrderFactory.php');
		require_once(INTEGRATION_PATH . 'Orders.php');
		require_once(INTEGRATION_PATH . 'OrdersProducts.php');
		require_once(INTEGRATION_PATH . 'Coupons.php');
		require_once(INTEGRATION_PATH . 'ProductsDAO.php');

		ServiceLocator::registerInstance(User::getInstance());
		ServiceLocator::registerFactory('ConnectionFactory');
		ServiceLocator::registerInstance(Configuration::getInstance());

		ServiceLocator::registerInstance(new Currencies());
		ServiceLocator::registerInstance(new Prices());
		ServiceLocator::registerInstance(new Coupons());
		ServiceLocator::registerInstance(new Taxes());
		ServiceLocator::registerInstance(new OrdersProducts());
		ServiceLocator::registerInstance(new Orders());
		ServiceLocator::registerInstance(new ProductsDAO());
		ServiceLocator::registerFactory('OrderFactory');
		ServiceLocator::registerInstance(new User());

		$this->products = ServiceLocator::getService('ProductsDAO');
		$this->orderfactory = ServiceLocator::getService('OrderFactory');
		$this->user = ServiceLocator::getService('User');
	}
	
	/**
	* Find a Product or Array of products in the database.
	* @returns Product Transfer Object
	*/
	public function getProduct($id) {
		return $this->products->find($id);
	}
	
	/**
	* Makes an order based on values in the Order Transfer Object
	* @returns true if order was successful
	*/
	public function makeOrder(OrderDTO $dto) {
		if ($this->user->authenticate()) {
			if ($this->orderfactory->execute($dto)) return true;
			else return false;
		} else throw new Exception("Could not authenticate, please contact the system administrator.");
	}
	
	/**
	* Allows Logging in based on the Cashier Transfer Object
	* @returns true if order was successful
	*/
	public function login(CashierDTO $dto) {
		return false;
	}
}
?>